package Collection_Inte;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 定义一个Employee类，该类包含：private成员属性 name,age 要求：
 * 1·创建3个 Employee 对象放入 HashSet中
 * 2.当 name和age的值相同时，认为是相同员工，不能添加到HashSet集合中
 */

@SuppressWarnings("all")
public class Set_ {

    public static void main(String[] args) {
        HashSet set = new HashSet();
        set.add(new Employee("tom", 21));
        set.add(new Employee("sa", 19));
        set.add(new Employee("ge", 20));
        set.add(new Employee("ge", 20));
        set.add(new Employee("ge", 20));
        System.out.println(set);
    }

}

class Employee{

    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age && Objects.equals(name, employee.name);
    }
}
